package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shouhuobao.station.R;
import com.shouhuobao.take.model.PostCompanyBean;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.lang.Str;

/**
 * 选择快递公司适配器
 */
public class ParcelExpressCompanyAdapter extends BaseAdapterExt<PostCompanyBean> {
    private ViewHolder viewHolder;
    private int commonSize = 0;

    public ParcelExpressCompanyAdapter(ArrayList<PostCompanyBean> items, Activity context, int commonSize) {
        super(items, context);
        this.commonSize = commonSize;
    }

    @Override
    public boolean isEnabled(int position) {
        if (items.get(position).getName().length() == 1 || (commonSize == 0 && "常用快递".equals(items.get(position).getName()))) {// 如果是字母索引
            return false;// 表示不能点击
        }
        return super.isEnabled(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        viewHolder = new ViewHolder();
        PostCompanyBean item = items.get(position);
        if (item.getName().length() == 1 || (commonSize == 0 && "常用快递".equals(item.getName()))) {// 判断公司名字长度为1，说明是字母，加载字母的布局，否则加载公司名称和logo的布局
            convertView = LayoutInflater.from(context).inflate(R.layout.take_companycompany_item, parent, false);
            viewHolder.indexTv = (TextView) convertView.findViewById(R.id.indexTv);
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_selectcompany_item, parent, false);
            viewHolder.itemTv = (TextView) convertView.findViewById(R.id.itemTv);
            viewHolder.companyphotoIV = (ImageView) convertView.findViewById(R.id.companyphotoIV);
        }
        if (item.getName().length() == 1 || "常用快递".equals(item.getName())) {
            viewHolder.indexTv.setText(item.getName());
        } else {
            viewHolder.itemTv.setText(item.getName());
            //            viewHolder.companyphotoIV.setImageResource(item.getImageRes());
            if (Str.isNotEmpty(item.getImage())) {
                viewHolder.companyphotoIV.setImageResource(context.getResources().getIdentifier(item.getImage(), "drawable", context.getPackageName()));
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView indexTv;
        private TextView itemTv;
        private ImageView companyphotoIV;
    }

}
